package com.moneywelcome.orderassembly.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.orderassembly.mapper.GoldCommentMapper;
import com.moneywelcome.orderassembly.model.GoldComment;
import com.moneywelcome.orderassembly.service.GoldCommentService;
import com.moneywelcome.orderassembly.service.GoldReplyService;
import com.moneywelcome.orderfacade.dto.CommentDto;
import com.moneywelcome.orderfacade.dto.ReplyDto;
import com.moneywelcome.orderfacade.vo.CommentVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author alice
 * @since 2022-01-10
 */
@Service
public class GoldCommentServiceImpl extends ServiceImpl<GoldCommentMapper, GoldComment> implements GoldCommentService {
    @Autowired
private GoldReplyService grs;
    @Override
    public PageResult<CommentDto> loadComment(String goldId) {
        QueryWrapper<GoldComment> qw = new QueryWrapper<>();
        qw.eq("gold_id",goldId);
        Page<GoldComment> page = page(new Page<>(1, 3), qw);
        List<GoldComment> records =page.getRecords();
        //获得评论id
        List<Integer> commentIds = records.stream().map(GoldComment::getCommentId).collect(Collectors.toList());
        if(null==commentIds||commentIds.size()==0) throw new MwcArgsException("没有评论记录",StatusCode.Error);
        Map<Integer, List<ReplyDto>> map = grs.loadReply(commentIds);
        //结果封装成树形并按热度排序
        List<CommentDto> commentDtos = BeanUtil.copyToList(records, CommentDto.class);
        commentDtos.stream().sorted(Comparator.comparing(CommentDto::getCommentLevel)).forEach(commentDto -> {
            commentDto.setChildren(map.get(commentDto.getCommentId()));
        });
        PageResult pageResult = BeanUtil.copyProperties(page, PageResult.class);
        pageResult.setData(commentDtos);
        pageResult.setTotalCount(page.getTotal());
        return pageResult;
    }

    @Override
    public CommentDto create(CommentVo commentVo) {
        GoldComment comment = BeanUtil.copyProperties(commentVo, GoldComment.class);
        comment.setCommentTime(DateUtil.now());
        if (!save(comment)) throw new MwcArgsException("发表评论失败", StatusCode.Error);
        return BeanUtil.copyProperties(comment,CommentDto.class);
    }

    @Override
    public CommentDto levelUp(Integer commentId) {
        GoldComment comment = getOne(new QueryWrapper<GoldComment>().eq("comment_id",commentId));
        System.out.println(comment);
        comment.setCommentLevel(comment.getCommentLevel()+1);//热度加一
        saveOrUpdate(comment);
        return BeanUtil.copyProperties(comment,CommentDto.class);
    }
}
